﻿/**
* ggCom class (Actionscript 3)
* Used to pass data to Glanceguide
*
* Copyright (c) 2008 Glanceguide, Inc.  All rights reserved. 
*    http://glanceguide.com
* .  Decompiling, reverse engineering, copying or unauthorized redistribution is prohibited.    
**/

package com.nielsen
{
	import flash.system.Security;
	import flash.events.IEventDispatcher;
	import flash.events.Event;
	import flash.events.StatusEvent;
	import flash.events.IOErrorEvent;
	import flash.external.ExternalInterface;
	import flash.net.LocalConnection;
	import flash.net.URLRequest;
	import flash.display.Loader;
	import flash.utils.setTimeout;
	import flash.display.MovieClip;
	
	public class ggCom extends MovieClip 
 {

   // ********************** Start Customer Editable variables and functions. DO NOT EDIT ABOVE THIS LINE ************************************** 
   public var _nolggGlobalParams:Object = {
      clientid:"es-202596",                     // Required; Nielsen assigned client ID
      vcid:    "b01",                     // Required; Nielsen assigned vcid
      msgint: ""                                // Optional. to specify additional messages per stream. by default, start and end streams get a msg each
   }; 
   		
		// set to true is Javascript interface is needed for calls to gg; if enableJSInterface is set to true and you use <embed> tag for your player, 
		// make sure that "allowScriptAccess" parameter is set to "always". Runtime error will happen otherwise. 
		private var enableJSinterface: Boolean = false;   
		public function getUserID( ) : String
		{
			return null;   // optionally set userid here (max 32 chars)
		}
		// Additional custom video information can be sent to Glance Guide by using this method.  Call it each time before a load video event occurs.
		// NOTE: Use the following xml syntax: <p1>value</p1> .... <pn>value</pn>
		function setVideoInfo( videoInfo ) : void
		{
			PM( "videoInfo", videoInfo );
		};	
   
//*
// ********************** End Customer Editable variables and functions. DO NOT EDIT BELOW THIS LINE ************************************** 
//*
   //additional config parameters (set by NOL/GG)
   function setNOLconfigparams() {
     _nolggGlobalParams.sfcode = "uk";         
     _nolggGlobalParams.cisuffix = "";       
     _nolggGlobalParams.prod = "sc";       
   }
   
		private static var ggComInstance:ggCom = null;
		private static var ggComCreate:Boolean = false;
		private var GGSWFCONNECTIONRETRY:Number = 5;
		private var GGSWFLOADRETRY:Number = 2;		
		private var GGMAXQUEUESIZE:Number = 10000;  
   private var GGSWFADDRESS:String;  
		private var GGSWFLOADERRORADDRESS:String = "http://dz.glanceguide.com/errl.php";			
		private var ggPlayerType = "gen3flvplayer";								
		private var ggConnection:LocalConnection = new LocalConnection( );
		private var ggConnectionName:String;
		private var ggConnectionRetry:Number = 0;
		private var ggLoadRetry:Number = 0;		
		private var ggLoader:Loader = new Loader( ); 
		private var ggEventQueue:Array = new Array( );
		private var ggLoaded:Boolean = false;
		private var ggHasStage:Boolean = false;
		public function PCM( functionType, ...param ) : void
		{
			PM( functionType, param[0], param[1], param[2], param[3] ); 
		}	
		
		public function PM( functionType, ...param ) : void
		{
			if ( ggLoaded )
				ggConnection.send( ggConnectionName, "ggProcessMetricAC3", functionType, new Date( ).getTime( ), param[0], param[1], param[2], param[3] );
			else if ( ggEventQueue.length < GGMAXQUEUESIZE )
			{
				ggEventQueue.push( functionType );
				ggEventQueue.push( new Date( ).getTime( ));
				ggEventQueue.push( param[0] );
				ggEventQueue.push( param[1] );
				ggEventQueue.push( param[2] );
				ggEventQueue.push( param[3] );
			}
		}
		
		public static function getInstance( ) : ggCom
		{
			if ( ggComInstance == null )
			{
				ggComCreate = true;
				ggComInstance = new ggCom( );
				ggComCreate = false;
			}
			
			return ggComInstance;
		}
		public function setStage( stageConnector:Object )
		{
			stageConnector.stage.addChild( this );
			ggHasStage = true;
		}
		public function ggCom( ) 
		{ 
			if ( !ggComCreate )   
        return; 			
     setNOLconfigparams();         
     GGSWFADDRESS = "http://secure-" + _nolggGlobalParams.sfcode + ".imrworldwide.com/novms/gn/3/ggce302.swf"; 
			Security.allowDomain( GGSWFADDRESS );
			Security.allowInsecureDomain( GGSWFADDRESS );
     if (enableJSinterface && ExternalInterface.available)			
			    ExternalInterface.addCallback( "ggPCM", PCM );
			ggConnection.addEventListener( StatusEvent.STATUS, onGGSwfConnectionStatus );
			loadGGSWF( );
		}		
		
		private function loadGGSWF( )
		{
			ggLoader = new Loader( ); 
			ggLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, onLoadGGSwfHandler );
			ggLoader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, onLoadGGSwfIoErrorHandler );
			ggLoader.load( new URLRequest( GGSWFADDRESS ));						
		}
				
		private function emptyEventQueue( ) : void
		{
			var queueLength = ggEventQueue.length;
			for( var i = 0; i < queueLength; i += 6 )
				ggConnection.send( ggConnectionName, "ggProcessMetricAC3", ggEventQueue[ i ], ggEventQueue[ i + 1 ], ggEventQueue[ i + 2 ], ggEventQueue[ i + 3 ], ggEventQueue[ i + 4 ], ggEventQueue[ i + 5 ] );
				  
			ggEventQueue.length = 0;
		}
		private function makeGGSwfConnection( )
		{
			if ( ggConnectionRetry < GGSWFCONNECTIONRETRY )
			{
       var paramStr= ""; 
       var name;
			  if (ggHasStage) {
          for (name in _nolggGlobalParams)  
             if (root.loaderInfo.parameters["nolgg_"+name] != null && root.loaderInfo.parameters["nolgg_"+name] != "") { 
                 _nolggGlobalParams[name] = root.loaderInfo.parameters["nolgg_" + name];
             }
			     PM(51,root.loaderInfo.url);
       
       }
       for (name in _nolggGlobalParams)
          paramStr += "<" + name + ">" + _nolggGlobalParams[name] + "</" + name + ">";  
				ggConnectionName = ggLoader.content.name;				
				ggConnection.send( ggConnectionName, "ggInitializeAC3", ggPlayerType, getUserID( ), paramStr);
			}
		}		
		
		private function ggSwfConnectionSuccess( )
		{
			ggLoaded = true;
			emptyEventQueue( );			
		}		
		
		private function onGGSwfConnectionStatus( event:StatusEvent ) : void 
		{
			if ( ggLoaded ) return;
			
     switch ( event.level ) 
			{
				case "status":
					  ggSwfConnectionSuccess( );
          break;
      	case "error":
          ggConnectionRetry++;
          makeGGSwfConnection( );
          break;
     }
   }
		
		private function onLoadGGSwfHandler( event:Event ) : void
		{
			this.addChild( ggLoader );	
			setTimeout( makeGGSwfConnection, 500 ); 			
		}	
		
		private function onLoadGGSwfIoErrorHandler( event:IOErrorEvent ) : void 
		{
			if ( ggLoadRetry < GGSWFLOADRETRY )
			{
				ggLoadRetry++;
				loadGGSWF( );
			}
			else
				sendLoadGGSWFFail( );			
		}

		private function sendLoadGGSWFFail( )
		{
		  ggLoader = new Loader( ); 
			ggLoader.load( new URLRequest( GGSWFLOADERRORADDRESS + "?clientid=" + _nolggGlobalParams.clientid + "&code=ggLoadFailed&cts=" + new Date( ).getTimezoneOffset( ) / -60 + "," + new Date( ).getTime( )));
		}
 }
    
} 
